﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BadHabit.Factory.Windows {

    /// <summary>
    /// THIS SUCKS.
    /// Its is just a fix until i have time to make something decent.
    /// </summary>
    public partial class NewMaintanceProject : BadHabit.WPFTools.ShapedWindow {

                        
        public static DependencyProperty DurationProperty = DependencyProperty.Register(
            "Duration", typeof(int), typeof(NewMaintanceProject));

        
        public int Duration {
            get { return (int)GetValue(DurationProperty); }
            set { SetValue(DurationProperty, value); }
        }


        public NewMaintanceProject() {
            InitializeComponent();
        }

        public NewMaintanceProject(Data.ProjectDataSet dsProject) {
            InitializeComponent();
            dsProject.Project[0].EstimatedStartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DataContext = dsProject;
            Duration = 12;
        }

        protected override void OnClosed(EventArgs e) {
            
            GenerateBacklogItems();

            base.OnClosed(e);

            if (Client.FactoryMainWindow != null)
                Client.FactoryMainWindow.IsPopUpOpened = false;
        }

        private void ShapedWindow_Loaded(object sender, RoutedEventArgs e) {
            Client.FactoryMainWindow.IsPopUpOpened = true;            
        }


        /// <summary>
        /// Generates backlog itens for each month of theproject.
        /// </summary>
        /// <param name="monthQuantity"></param>
        /// <param name="iniDate"></param>
        public void GenerateBacklogItems() {

            Data.ProjectDataSet dsProject = DataContext as Data.ProjectDataSet;

            if (dsProject == null)
                return;

            DateTime startDate = dsProject.Project[0].EstimatedStartDate;
            
            short BacklogItemNumber = 1;

            DateTime endDate = startDate.AddMonths(1);

            for (short i = 0; i < Duration; i++) {
                BacklogItemNumber = (short)(i + 1);

                endDate = startDate.AddMonths(i + 1);

                Data.ProjectDataSet.SprintRow re = dsProject.Sprint.AddSprintRow(
                    Guid.NewGuid().ToString().ToLower(),
                    dsProject.Project[0],
                    (short)(i + 1),
                    "Sprint " + (i + 1),
                    DateTime.Now,
                    endDate,
                    0,
                    DateTime.Now);

                Data.ProjectDataSet.BacklogItemRow r =
                    dsProject.BacklogItem.AddBacklogItemRow(
                        Guid.NewGuid().ToString().ToLower(),
                        BacklogItemNumber,
                        dsProject.Project[0],
                        startDate.AddMonths(i).ToString("yyyy MMMM"),
                        "",
                        (int)BacklogItemStatus.REQUIRED,
                        "Hour Bank Tasks",
                        (short)startDate.AddMonths(i).Year,
                        (short)startDate.AddMonths(i).Month,
                        0,
                        0,
                        DateTime.MinValue,
                        DateTime.MinValue,
                        DateTime.Now,
                        0,
                        DateTime.MinValue,
                        DateTime.MinValue,
                        null,
                        DateTime.MinValue,
                        null,
                        false,
                        (short)BacklogItemCancelReasons.UNKNOW);

                r.SetStartDateNull();
                r.SetFinishDateNull();
                r.SetSizeItemTypeNull();
                r.SetAcceptedDateNull();
                r.SetVerifiedDateNull();
                r.SetValidatedDateNull();
                


                foreach (Data.ProjectDataSet.BacklogItemUnitRow unit in dsProject.BacklogItemUnit) {
                    dsProject.BacklogItemHours.AddBacklogItemHoursRow(Guid.NewGuid().ToString().ToLower(), r, dsProject.Project[0].ProjectUId, unit.UnitName, 0, re, 0);
                }
            }

          

        }


    }
}
